package logic

import (
	"context"

	"server-go/rpc/reward/internal/models"
	"server-go/rpc/reward/internal/svc"
	"server-go/rpc/reward/types/reward"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type AdminLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAdminLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminLoginLogic {
	return &AdminLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理员登录
func (l *AdminLoginLogic) AdminLogin(in *reward.AdminLoginInput) (*reward.AdminLoginOutput, error) {
	tx := l.svcCtx.Db
	out := &reward.AdminLoginOutput{
		Msg:    "no admin",
		Status: 400,
	}
	if in.AdminPassword == "" || in.AdminUsername == "" {
		return out, nil
	}
	loginDao := &models.LoginDao{Username: in.AdminUsername, Password: in.AdminPassword}
	if err := loginDao.First(tx); err != nil && err != gorm.ErrRecordNotFound {
		out.Msg = err.Error()
		out.Status = 400
		return out, err
	} else if err == gorm.ErrRecordNotFound {
		return out, nil
	}
	out.Msg = "login success"
	out.Status = 200
	return out, nil
}
